var row_delimiter="\n";
var column_delimiter=",";
function DataObjectGeneratorDataSet() {

	if (!(this instanceof DataObjectGeneratorDataSet)) return new DataObjectGeneratorDataSet(); // to protect from bugs when instantiate happens without new keyword

	var data = [
	{name:"user",	
		renew:function(callback){	
			var table = this;
			$.ajax({
				type: "POST",					
				url: "/dbcode/UserRead.php"
			}).done(function(response){					
				var parsed_response = JSON.parse(response);				
				table.rows = parsed_response;
				if(typeof(callback)!='undefined')
				callback();
			});
		},		
		/*INSER INTO User (Id,Nick,Password) VALUES ($id[i], $Nick[i], $Password[i]);
		params - complex object - {id0:'1',Nick0:'bohdan',Password0:'test',
								   id1:'2',Nick1:'Taras',Password1:'trorlolo',...}*/
		insert:function(params,callback){ 									
			$.ajax({
				type: "POST",					
				data:params,
				url: "/dbcode/UserWrite.php"
			}).done(function(response) {				
				callback(response);
			});
		},
		/*UPDATE User SET $datafield[i]=$value[i] WHERE $keyfield[i] $operator $key[i]; // $operator = '=' if not set;
		params - complex object - {datafield0:'Nick',value0:'Mickey', keyfield0:'id', operator0:'!=', key0:'23',
								   datafield1:'Password',value1:'Looser', keyfield1:'Nick', operator1:'LIKE', key1:'User',...}*/ 								
		update:function(params,callback){
			$.ajax({
				type: "POST",					
				data:params,
				url: "/dbcode/UserUpdate.php"
			}).done(function(response) {											
				callback(response);
			});				
		},				
		
		/*DELETE FROM User WHERE $keyfield[i] $operator[i] $key[i]; // $operator[i] = '=' if not set; 
		params - complex object - {keyfield0:'Nick',operator0:'!=',key0:'1'
								   keyfield1:'Id',key1:'123',...}*/ 											
		remove:function(params,callback){
			$.ajax({
				type: "POST",					
				data:params,
				url: "/dbcode/UserDelete.php"
			}).done(function(response) {											
				callback(response);
			});				
		},
		
//each		
		eachRow:function(callback){//foreach row
			for(var i=0;i<this.rows.length;i++){
				callback(this.rows[i]);
			}
		},
		eachCol:function(callback){//foreach col
			for(var i=0;i<this.columns.length;i++){
				callback(this.columns[i]);
			}
		},
//data		
		columns:["Id", "Nick","Password"], rows:["empty row"]
	}];
	this.table = function(name) {
		for(var i=0; i<data.length; i++) {
			if(data[i].name == name) {
				return data[i];
			}
		}
		return null;
	}
	this.rows = function(name) {
		var table = this.table(name);
		if(table != null) {
			return table.rows;
		}
		return null;
	}
	this.columns = function(name) {
		var table = this.table(name);
		if(table != null) {
			return table.columns;
		}
		return null;
	}
	this.column = function(tableName, columnName) {
		var table = this.table(tableName);
		if(table != null) {
			var columnIndex = -1;
			var columns = this.columns(tableName);
			for(var i=0;i<columns.length;i++) {
				if( columns[i] == columnName ) {
					columnIndex = i;
				}
			}

			if(columnIndex != -1) {
				var rows = this.rows(tableName);
				var values = new Array(rows.length);
				for(var i=0;i<rows.length;i++) {
					values[i] = rows[i][columnIndex];
				}
				return values;
			} else {
				return null;
			}
		}
		return null;
	}	
}

